home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / gengetopt-2.6.lha / gengetopt-2.6 / src / gengetopt.c < prev    next >
C/C++ Source or Header  |  2002-03-02  |  8KB  |  332 lines

  1. /**
  2.  * Copyright (C) 1999, 2000, 2001  Free Software Foundation, Inc.
  3.  *
  4.  * This file is part of GNU gengetopt 
  5.  *
  6.  * GNU gengetopt is free software; you can redistribute it and/or modify 
  7.  * it under the terms of the GNU General Public License as published by 
  8.  * the Free Software Foundation; either version 2, or (at your option) 
  9.  * any later version. 
  10.  *
  11.  * GNU gengetopt is distributed in the hope that it will be useful, but 
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  14.  * Public License for more details. 
  15.  *
  16.  * You should have received a copy of the GNU General Public License along 
  17.  * with gengetopt; see the file COPYING. If not, write to the Free Software 
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. extern int yyparse () ;
  26.  
  27. void gengetopt_free (void);
  28.  
  29. #include "argsdef.h"
  30. #include "ggos.h"
  31. #include "gm.h"
  32.  
  33. /* The following one is generated by gengetopt itself */
  34. #include "cmdline.h"
  35.  
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39.  
  40. #include "gengetopt.h"
  41.  
  42. #ifndef HAVE_STRDUP
  43. extern char *strdup (const char *s) ;
  44. #endif
  45.  
  46. #define DEFAULT_HEADER_EXT "h"
  47. #define DEFAULT_C_EXT "c"
  48.  
  49. struct gengetopt_option * gengetopt_options = NULL;
  50. char * gengetopt_package = NULL;
  51. char * gengetopt_version = NULL;
  52. char * gengetopt_purpose = NULL;
  53. int gengetopt_count_line = 1;
  54.  
  55. int canonize_vars (void);
  56.  
  57. extern int reportbugs_text_length;
  58. extern char *reportbugs_text[];
  59. extern int copyright_text_length;
  60. extern char *copyright_text[];
  61.  
  62. static void print_copyright();
  63. static void print_reportbugs();
  64.  
  65. int
  66. main (int argc, char **argv)
  67. {
  68.   struct gengetopt_args_info args_info ;
  69.   char *cmdline_parser_name ; /* name of the generated function */
  70.   char *cmdline_filename ; /* name of generated file */
  71.   char **comment ;
  72.   int i, len ;
  73.   FILE *input_file ;
  74.  
  75.   if (cmdline_parser (argc, argv, &args_info) != 0)
  76.     {
  77.       fprintf (stderr, "Run gengetopt --help to see the list of options.\n");
  78.       exit(1) ;
  79.     }
  80.  
  81.   if (args_info.help_given)
  82.   {
  83.     printf ("GNU ");
  84.     cmdline_parser_print_help ();
  85.     print_reportbugs ();
  86.     exit (0);
  87.   }
  88.  
  89.   if (args_info.version_given)
  90.   {
  91.     printf ("GNU ");
  92.     cmdline_parser_print_version ();
  93.     print_copyright ();
  94.     exit (0);
  95.   }
  96.  
  97.   cmdline_parser_name = args_info.func_name_arg ;
  98.   cmdline_filename = args_info.file_name_arg ;
  99.  
  100.   switch  (gengetopt_add_option ("help", 'h', "Print help and exit", ARG_NO, 0, 0, 0))
  101.   {
  102.   case 1: fprintf (stderr, "gengetopt: line %d: not enough memory\n",
  103.            gengetopt_count_line);
  104.       return 1;
  105.   case 2:
  106.   case 3:
  107.   case 4: fprintf (stderr, "gengetopt: bug found in %s:%d!!\n", __FILE__, __LINE__);
  108.       abort ();
  109.   }
  110.   switch  (gengetopt_add_option ("version", 'V', "Print version and exit", ARG_NO, 0, 0, 0))
  111.   {
  112.   case 1: fprintf (stderr, "gengetopt: line %d: not enough memory\n",
  113.            gengetopt_count_line);
  114.       return 1;
  115.   case 2:
  116.   case 3:
  117.   case 4: fprintf (stderr, "gengetopt: bug found in %s:%d!!\n", __FILE__, __LINE__);
  118.       abort ();
  119.   }
  120.  
  121.   if ( args_info.input_arg )
  122.     {
  123.       input_file = freopen (args_info.input_arg, "r", stdin) ;
  124.       if (!input_file)
  125.         {
  126.           fprintf (stderr, "Error opening input file: %s\n", 
  127.                    args_info.input_arg);
  128.           exit (1);
  129.         }
  130.     } /* else standard input is used */
  131.  
  132.   if (yyparse ()) {
  133.     gengetopt_free ();
  134.     return 1;
  135.   }
  136.  
  137.   if (canonize_vars ()) {
  138.     gengetopt_free ();
  139.     return 1;
  140.   }
  141.  
  142.   comment = (char **)( malloc ( 3*sizeof(char *) ) ) ;
  143.   /* one for our added line, and one for termination string */
  144.   comment[0] = "generated with the following command:" ;
  145.   len = 0 ;
  146.   for ( i = 0; i < argc ; ++i )
  147.     len += strlen (argv[i]) + 1 ; /* +1 is for added space */
  148.  
  149.   comment[1] = (char *) malloc (len * sizeof (char)) ;
  150.   comment[1][0] = 0;
  151.  
  152.   for ( i = 0; i < argc ; ++i )
  153.     {
  154.       strcat (comment[1], argv[i]) ;
  155.       strcat (comment[1], " ") ;
  156.     }
  157.   comment[2] = NULL ;
  158.  
  159.   if (generate_cmdline_parser (cmdline_parser_name,
  160.                                args_info.unamed_opts_given,
  161.                                cmdline_filename,
  162.                                DEFAULT_HEADER_EXT,
  163.                                DEFAULT_C_EXT,
  164.                                args_info.long_help_given,
  165.                                args_info.no_handle_help_given,
  166.                                args_info.no_handle_version_given,
  167.                                args_info.no_handle_error_given,
  168.                                comment))
  169.     {
  170.     gengetopt_free ();
  171.     return 1;
  172.     }
  173.  
  174.   gengetopt_free ();
  175.  
  176.   return 0;
  177. }
  178.  
  179.  
  180. /* ************* */
  181.  
  182. int
  183. gengetopt_define_package (char * s)
  184. {
  185.     gengetopt_package = strdup (s);
  186.     if (gengetopt_package == NULL)
  187.         return 1;
  188.     return 0;
  189. }
  190.  
  191. int
  192. gengetopt_define_version (char * s)
  193. {
  194.     gengetopt_version = strdup (s);
  195.     if (gengetopt_version == NULL)
  196.         return 1;
  197.     return 0;
  198. }
  199.  
  200. int
  201. gengetopt_define_purpose (char * s)
  202. {
  203.   gengetopt_purpose = strdup (s);
  204.   if (gengetopt_purpose == NULL)
  205.     return 1;
  206.   return 0;
  207. }
  208.  
  209. int
  210. gengetopt_add_option (char * long_opt, char short_opt, char * desc,
  211.               int type, int flagstat, int required,
  212.                       char * default_value)
  213. {
  214.     struct gengetopt_option * n;
  215.  
  216.     if (long_opt == NULL || long_opt[0] == 0 ||
  217.         desc == NULL)
  218.         return 4;
  219.  
  220.     /* search for collisions */
  221.     for (n = gengetopt_options; n != NULL; n = n->next)
  222.     {
  223.         if (!strcmp (n->long_opt, long_opt)) return 2;
  224.         if (short_opt
  225.             && n->short_opt == short_opt)
  226.             return 3;
  227.     }
  228.  
  229.     n = malloc (sizeof (struct gengetopt_option));
  230.     if (n == NULL) return 1;
  231.  
  232.     n->long_opt = strdup (long_opt);
  233.     if (n->long_opt == NULL) {
  234.         free (n);
  235.         return 1;
  236.     }
  237.  
  238.     n->desc = strdup (desc);
  239.     if (n->desc == NULL) {
  240.         free (n->long_opt);
  241.         free (n);
  242.         return 1;
  243.     }
  244.     
  245.     n->short_opt = ((short_opt == '-') ? 0 : short_opt);
  246.     n->type = type;
  247.     n->flagstat = flagstat;
  248.         n->required = required;
  249.         n->default_string = 0;
  250.         n->default_given = (default_value != 0);
  251.         if (n->default_given)
  252.           {
  253.             if (type == ARG_STRING)
  254.               n->default_string = strdup (default_value);
  255.             else
  256.               n->default_num = atof (default_value);
  257.           }
  258.     n->next = NULL;
  259.     n->var_arg = NULL;
  260.  
  261.     /* if empty stack */
  262.     if (gengetopt_options == NULL)
  263.         gengetopt_options = n;
  264.     else {
  265.         struct gengetopt_option * p = gengetopt_options;
  266.         while (p->next != NULL) p = p->next;
  267.         p->next = n;
  268.     }
  269.     return 0;
  270. }
  271.  
  272.  
  273. void
  274. gengetopt_free (void)
  275. {
  276.   struct gengetopt_option *p, *pnext;
  277.  
  278.   if (gengetopt_package != NULL) free (gengetopt_package);
  279.   for (p = gengetopt_options; p != NULL; p = pnext)
  280.   {
  281.     pnext = p->next;
  282.     if (p->long_opt != NULL) free (p->long_opt);
  283.     if (p->desc != NULL) free (p->desc);
  284.     if (p->var_arg != NULL) free (p->var_arg);
  285.     free (p);
  286.   }
  287. }
  288.  
  289.  
  290. int
  291. canonize_vars (void)
  292. {
  293.   struct gengetopt_option *p;
  294.   char *pvar;
  295.  
  296.   if (gengetopt_options == NULL) {
  297.     printf ("gengetopt: bug found in %s:%d!!\n", __FILE__, __LINE__);
  298.     abort ();
  299.   }
  300.  
  301.   for (p = gengetopt_options; p != NULL; p = p->next)
  302.   {
  303.     p->var_arg = strdup (p->long_opt);
  304.     if (p->var_arg == NULL) {
  305.     printf ("gengetopt: not enough memory to canonize vars\n");
  306.     return 1;
  307.     }
  308.  
  309.     for (pvar = p->var_arg; *pvar; pvar++)
  310.       if (*pvar == '.' || *pvar == '-') *pvar = '_';
  311.   }
  312.   return 0;
  313. }
  314.  
  315. void
  316. print_copyright()
  317. {
  318.   int i;
  319.  
  320.   for (i = 1; i <= copyright_text_length; ++i)
  321.     printf("%s\n", copyright_text[i]);
  322. }
  323.  
  324. void
  325. print_reportbugs()
  326. {
  327.   int i;
  328.  
  329.   for (i = 1; i <= reportbugs_text_length; ++i)
  330.     printf("%s\n", reportbugs_text[i]);
  331. }
  332.